package com.dengc.springboot.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dengc.springboot.common.Constants;
import com.dengc.springboot.common.Result;
import com.dengc.springboot.entity.Dict;
import com.dengc.springboot.service.IDictService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

import com.dengc.springboot.service.IMeunService;
import com.dengc.springboot.entity.Menu;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dengc
 * @since 2023-04-26
 */
@RestController
@RequestMapping("/meun")
public class MeunController {

        @Resource
        private IMeunService meunService;

        @Resource
        private IDictService dictService;

        @PostMapping
        public Result save(@RequestBody Menu meun) {
            return Result.success(meunService.saveOrUpdate(meun));
        }

        @DeleteMapping("/{id}")
        public Result delete(@PathVariable Integer id) {
            return Result.success(meunService.removeById(id));
        }

        @GetMapping
        public Result findAll(@RequestParam(defaultValue = "") String name) {
                //返回一级菜单
                return Result.success(meunService.findMenus(name));
        }

        @GetMapping("/{id}")
        public Result findOne(@PathVariable Integer id) {
            return Result.success(meunService.getById(id));
        }

        //批量删除
        @PostMapping("/del/batch")
        public Result deleteBatch(@RequestBody List<Integer> ids){
                return Result.success(meunService.removeByIds(ids));
        }

        @GetMapping("/page")
        public Result findPage(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize,
                               @RequestParam(defaultValue = "") String name) {
                LambdaQueryWrapper<Menu> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.like(!"".equals(name), Menu::getName,name);
                queryWrapper.orderByDesc(Menu::getId);
                return Result.success(meunService.page(new Page<>(pageNum, pageSize),queryWrapper));
        }

        //查询所有的图标
        @GetMapping("/icons")
        public Result getIcons() {
                QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("type", Constants.DICT_TYPE);
                return Result.success(dictService.list(queryWrapper));
        }
}
